﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MonitorPlan.Admin
{
    public partial class Notification : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userid"] == null)
                {
                    Response.Redirect("/Admin");
                }
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 1;
            this.actionSms.InnerHtml = "";
        }

        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            string userid = this.ddlUser.SelectedValue;
            dsHouse.SelectCommand = "select houseid, housename from house where userid=" + userid;
            this.ddlHouse.DataBind();
        }

        protected void ddlHouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            string hid = this.ddlHouse.SelectedValue;
            dsTool.SelectCommand = "select * from tool where houseid=" + hid;
            this.ddlTool.DataBind();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
            this.rp1.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.actionSms.InnerHtml = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string IsSms = this.rdSms.Checked ? this.rdSms.Text : this.rdSms1.Text;
            string IsSetAlarm = this.rdAlarm.Checked ? this.rdAlarm.Text : this.rdAlarm1.Text;

            string StartTime="", EndTime = "";
            if(IsSetAlarm=="Yes")
            {
                StartTime = this.txtH.Text + ":" + txtMinute.Text + ":00 " + ddlAMPM.SelectedValue;
                EndTime = txtHEnd.Text + ":" + txtMinuteEnd.Text + ":00 " + ddlAMPMEnd.SelectedValue;
            }


            string sms;
            Model.Notification not = new Model.Notification();
            not.UserId = Convert.ToInt32(this.ddlUser.SelectedValue);
            not.ToolId = this.ddlTool.SelectedValue;
            not.HouseId = Convert.ToInt32(this.ddlHouse.SelectedValue);
            not.WhenToNotify = this.ddlNotify.SelectedValue;
            not.IsSms = IsSms;
            not.IsAlarm = IsSetAlarm;
            not.StartTime = StartTime;
            not.EndTime = EndTime;
            if (not.ToolId != "")
            {
                if (not.Add())
                {
                    sms = "<div class='alert alert-success'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has been save successfully!";
                    sms += "</div>";
                    this.actionSms.InnerHtml = sms;
                }
                else
                {
                    sms = "<div class='alert alert-error'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has not been saved successfully. Try again!";
                    sms += "</div>";
                    this.actionSms.InnerHtml = sms;
                }
            }
            else
            {
                this.actionSms.InnerHtml = "<span style='color:red'>Please select a tool to add!</span>";
            }
        }

        protected void rdAlarm_CheckedChanged(object sender, EventArgs e)
        {
            if(this.rdAlarm.Checked==true)
            {
                this.pnAlarm.Enabled = false;
            }
           
        }

        protected void rdAlarm1_CheckedChanged(object sender, EventArgs e)
        {
            if(this.rdAlarm1.Checked==true)
            {
                this.pnAlarm.Enabled = true;
            }
        }
    }
}